package com.jwdf.uda.ui.activity.presenter;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.bean.UploadImage;
import com.jwdf.uda.event.GuideAddEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.GuideAddAction;
import com.jwdf.uda.ui.activity.view.IGuideAddView;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.StringUtils;
import com.jwdf.uda.utils.ToastUtils;

import java.util.List;

import retrofit2.Response;

/**
 * 类说明：
 */
public class GuideAddPresenter extends AUploadImagePresenter<IGuideAddView> {
    public GuideAddPresenter(IGuideAddView iView) {
        super(iView);
    }

    /**
     * 点击右上角按钮
     */
    public void onHeadActionClick(){
        String content = mIView.getContent();
        if (StringUtils.isNullEmpty(content)) {
            ToastUtils.show(getContext(), R.string.guide_content_isnull);
            return;
        }
        String designerId=mIView.getDesignerId();
        if (StringUtils.isNullEmpty(designerId)){
            ToastUtils.show(getContext(),"请选择搭配师");
            return;
        }

        mIView.getFragment().showProgressDialog();
        List<UploadImage> uploadImages = getUploadImageList();
        if(uploadImages != null && uploadImages.size() > 0){
            doUploadImagesAction();
        }else{
            doGuideAddAction(getImageTokenList());
        }
    }
    @Override
    protected List<UploadImage> getUploadImageList() {
        return mIView.getUploadImageList();
    }
    @Override
    protected void doUploadImage(UploadImage uploadImage) {
        mIView.setImageToken(uploadImage);
    }


    @Override
    protected void doUploadImagesSuccess(List<String> imageTokens) {
        doGuideAddAction(imageTokens);
    }
    protected void doGuideAddAction(List<String> imageTokens) {
        GuideAddAction action = GuideAddAction.newInstance(getContext(),mIView.getContent(), imageTokens,mIView.getDesignerId());
        mIView.getFragment().onNewRequestCall(action.request(new AHttpService.IResCallback<PbBean.PbResGuideAdd>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResGuideAdd> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if (mIView.getFragment().getView() != null) {
                    mIView.getFragment().closeProgressDialog();
                    if (HttpHelper.showError(getContext(), resultCode, baseServerInfo, R.string.error_operation)) {
                        return;
                    }

                    doGuideAddSuccess(response.body());
                }
            }
        }));
    }

    protected void doGuideAddSuccess(PbBean.PbResGuideAdd body) {
        mIView.showSuccessDialog(body.getGuideId());
        EventBusUtil.post(new GuideAddEvent());
    }
}
